<?php
/**
 * Created by PhpStorm.
 * User: dell
 * Date: 2015/9/4
 * Time: 11:42
 */

class site extends adminBase{
    function  __construct(){
        parent::__construct();
        $this->cms->setPageTitle("站点管理");
        $this->cms->setControlFile("admin/tpl/site/site_control.json");
        $admin_info=$this->user->getLoginInfo();
        $user=UserMode::init();
        $levels=$user->getLevels($admin_info['id']);
        if(!$user->checkLevels(16,$levels)){
            $r['message']="您不具有此权限！";
            View::displayAsHtml($r,"plugin/state/tips.php");
            exit();
        }
    }

    function indexTask($page){
        $url=WebRouter::init()->getQuestion("page=");
        $page=$page>=1? $page: 1;
        $countOfPage=20;
        $offset=($page-1)*$countOfPage;
        $site = SiteModule::init();
        list($result['site'],$site_total) = $site->getList($countOfPage,$offset);
        $result['pages']=new Page($page,$site_total,$countOfPage);
        $result['pages']->setPageCallback(function($page)use($url){
            return $url.$page;
        });
        $this->cms->tableScene($result,"admin/tpl/site/site_table.php");
    }

    function addTask(){
        $this->cms->formScene(array(),"admin/tpl/site/site_add.php");
    }

    function addSubmitTask($name,$map_address,$longitude,$latitude,$username,$email,$password){
        if(isset($name)&&!empty($name)){
            $admin = AdminModule::init('admin');
            if(!$admin->checkUsername(trim($username))){
                $site = SiteModule::init();
                $result = $site->add($name,$map_address,$longitude,$latitude,$username,$email,$password);
                if($result){
                    $r['message']="添加成功";
                    $r['url']=WebRouter::init()->getAction('index');
                }else{
                    $r['message']="添加失败，请重试";
                }
            }else{
                $r['message']="用户名不能重复";
            }
        }else{
            $r['message']="站点名不能为空";
        }
        View::displayAsHtml($r,"plugin/state/tips.php");
    }

}